package edu.infomatik.tundra.pages;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;

import edu.infomatik.tundra.data.DAOtundra;
import edu.infomatik.tundra.data.IBean;
import edu.infomatik.tundra.data.ILogicBean;
import edu.infomatik.tundra.data.IMockedBean;
import edu.infomatik.tundra.data.MockTundraData;
import edu.infomatik.tundra.data.MockWorkBean;
import edu.infomatik.tundra.logic.WorkBean;
import edu.infomatik.tundra.logic.WorkKinds;
import edu.infomatik.tundra.logic.security.SystemAccessBean;
import edu.infomatik.tundra.util.TundraConstants;

@SuppressWarnings("unused")
public class ProjectList {
	
	@SessionState
	private SystemAccessBean user;
	private boolean userExists;
	@Property
	@SessionState
	private WorkBean project;
	private List<WorkBean> wbList;
	@SessionState
	private Boolean newWork=new Boolean(false);
	
	String onActivate(){
		project=null;
		if (!userExists) return "Index";
		wbList = getAllProjects();
		return null;
	}
	public List<WorkBean> getAllProjects(){
		wbList= new ArrayList<WorkBean>();
		for (IBean wb: DAOtundra.getInstance().getAllBeans(new WorkBean())){
			if (((WorkBean)wb).getKind()==WorkKinds.PROJECT_VALUE){
				wbList.add((WorkBean)wb);
			}
		}
		return wbList;
	}
	@OnEvent(component="projectNameClick")
	String onNameClicked(long id){
		if (wbList.size()>0){
			for(WorkBean wb: wbList)
				if (wb.getId()==id) project=wb;
			return "TheProject";
		}
		return null;
	}
	@OnEvent(component="deleteProject")
	String onDeleteClicked(long id){
		if (wbList.size()>0){
			int i=0;
			for(WorkBean wb: wbList){
				if (wb.getId()==id) {
					DAOtundra.getInstance().delete(wb);
					wbList=getAllProjects();
					return "ProjectList";
				}else i++;
			}
		}
		return null;
	}
	@OnEvent(component="editProject")
	String onEditClicked(long id){
		project=null;
		if (wbList.size()>0){
			for(WorkBean wb: wbList)
				if (wb.getId()==id) project=wb;
			return "CreateProject";
		}
		return null;
	}
	
}
